package hrms.controller;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import hrms.entity.Train;
import hrms.service.TrainServiceImpl;
import hrms.util.LogUtil;
import hrms.util.Result;

@Controller
@RequestMapping("/train")
public class TrainController {
	
	@Resource(name="trainService")
	private TrainServiceImpl TrainService;
	
	@ExceptionHandler
	public String exHandle(Exception ex, HttpServletRequest req) {
		LogUtil.saveEx(ex);
		return "error";
	}
	
	@ResponseBody
	@RequestMapping("/loadTrain.sls")
	public Result<List<Train>> executeA(HttpServletRequest req ,HttpServletResponse res){
		String page = req.getParameter("page");
		Result<List<Train>> result = TrainService.findByPage(page,res);
		return result;
	}
	
	@ResponseBody
	@RequestMapping("/trainPage.sls")
	public Result<Map<String,Integer>> executeB2(HttpServletRequest req){
		Result<Map<String,Integer>> result = TrainService.pageTotal(req);
		return result;
	}
	
	
	@ResponseBody
	@RequestMapping("/addTrain.sls")
	public Result<Object> executeB3(HttpServletRequest req){
		String tcourse = req.getParameter("tcourse");
		String tcher = req.getParameter("tcher");
		String tstu = req.getParameter("tstu");
		String tplace = req.getParameter("tplace");
		String tdate = req.getParameter("tdate");
		String ttime = req.getParameter("ttime");
		return TrainService.addTrain( tcourse,  tcher, tstu, tplace,tdate,ttime);
	}
	
	/*
	@ResponseBody
	@RequestMapping("/updateTrain.sls")
	public Result<Object> executeB4(HttpServletRequest req){
		String TrainId = req.getParameter("TrainId");
		String TrainName = req.getParameter("TrainName");
		String TrainAge = req.getParameter("TrainAge");
		String TrainSex = req.getParameter("TrainSex");
		String TrainJob = req.getParameter("TrainJob");
		String TrainDept = req.getParameter("TrainDept");
		return TrainService.uppdateTrain(TrainId, TrainName,  TrainSex, TrainAge, TrainJob, TrainDept,req);
	}
	@ResponseBody
	@RequestMapping("/changeState.sls")
	public Result<Object> executeI(HttpServletRequest req){
		String TrainId = req.getParameter("id");
		return TrainService.changeState(TrainId, req);
	}
	
	@ResponseBody
	@RequestMapping("/showTrain.sls")
	public Result<Train> executeII(HttpServletRequest req){
		String TrainId = req.getParameter("TrainId");
		return TrainService.findTrain(TrainId, req);
	}
	*/
	@ResponseBody
	@RequestMapping("/searchTrain.sls")
	public Result<List<Train>> executeAA(HttpServletRequest req ,HttpServletResponse res){
		String name = req.getParameter("name");
		if(name == null || name == ""){
			return TrainService.findByPage("1",res);
		}
		return TrainService.search(name);
	}
}